/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.model.distribution.Distribution;
import lib.model.distribution.DistributionType;

/**
 * This class is used as a model to show distributions
 * It shows the most relevant properties of the distribution as type, mean and variance
 * It allows to add and remove distributions from the model, updating the view of the table
 * @see Distribution
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMDistribution extends DefaultTableModel {

    private List<Distribution> distributions;

    /**
     * Class builder
     * @param distributions The distribution list to show in the table
     */
    public TMDistribution(List<Distribution> distributions) {
        super();

        columnIdentifiers.add("Type");
        columnIdentifiers.add("Mean");
        columnIdentifiers.add("Variance");
        setDistributions(distributions);
    }

    public List<Distribution> getDistributions() {
        return distributions;
    }

    public void setDistribution(int index, Distribution distribution) {
        distributions.set(index, distribution);
        refresh();
    }

    public Distribution getDistrubution(int index) {
        return distributions.get(index);
    }

    public void setDistributions(List<Distribution> distributions) {
        this.distributions = distributions;
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        for (Distribution d : distributions) {
            v = new Vector();
            int meanHours = (int) d.getMean() / 3600000;
            int meanMinutes = (int) (d.getMean() % 60000) / 1000;
            String mean = String.valueOf(meanHours) + ":" + String.valueOf(meanMinutes);
            int varianceHours = (int) d.getVariance() / 3600000;
            int varianceMinutes = (int) (d.getVariance() % 60000) / 1000;
            String variance = String.valueOf(varianceHours) + ":" + String.valueOf(varianceMinutes);
            v.add(d.getType().toString());
            v.add(mean);
            if (d.getType() == DistributionType.STATIC) {
                v.add("-");
            } else {
                v.add(variance);
            }
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
